package com.mlm.mlmapp.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.mlm.mlmapp.R;
import com.mlm.mlmapp.utils.WebViewScriptInterface;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;

@EActivity(R.layout.show_web_view)
@SuppressLint("SetJavaScriptEnabled")
public class ShowWebView extends BaseActivity {

    @Extra
    String proxyInfoId = "";
    @Extra
    String url = "";
    @Extra
    String title = "";
    //private Button button;
    private WebView webView;
    WebChromeClient wvcc;
    @AfterViews
    void init(){
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        proxyInfoId = intent.getStringExtra("proxyInfoId");
        wvcc = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);

                ((TextView) findViewById(R.id.tv_title)).setText(title);
            }

        };
        webView = (WebView) findViewById(R.id.webView1);
        if (haveNetworkConnection()) {

            startWebView(url);
        } else {
//           webView.loadUrl("file:///android_asset/error.html");
            webView.loadUrl("file:///android_asset/error.html");
        }
    }
    private void startWebView(String url) {


        // 设置setWebChromeClient对象
        webView.setWebChromeClient(wvcc);

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                    startActivity(intent);
                } else {

                    view.loadUrl(url);

                }
                return true;
            }

        });

        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new WebViewScriptInterface(ShowWebView.this, ShowWebView.this, webView, proxyInfoId), "share");
        webView.loadUrl(url);

    }
    @Click
    void btn_back() {
        onBackPressed();
    }
    @Override
    // Detect when the back button is pressed
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            // Let the system handle the back button
            super.onBackPressed();
            Intent data = new Intent();
            setResult(RESULT_CANCELED, data);
            this.finish();
        }
    }
//    void init() {
//
//        wvcc = new WebChromeClient() {
//            @Override
//            public void onReceivedTitle(WebView view, String title) {
//                super.onReceivedTitle(view, title);
//
//                ((TextView) findViewById(R.id.tv_title)).setText(title);
//            }
//
//        };
//        Intent intent = getIntent();
//        url = intent.getStringExtra("url");
//        proxyInfoId = intent.getStringExtra("proxyInfoId");
//        title = intent.getStringExtra("title");
//
//        //Get webview
//        webView = (WebView) findViewById(R.id.webView1);
//        if (haveNetworkConnection()) {
//
//            startWebView(url);
//        } else {
////           webView.loadUrl("file:///android_asset/error.html");
//            webView.loadUrl("file:///android_asset/error.html");
//        }
//
//    }
//
//    private void startWebView(String url) {
//
//        //Create new webview Client to show progress dialog
//        //When opening a url or click on link
//        // 设置setWebChromeClient对象
//        webView.setWebChromeClient(wvcc);
//
//        webView.setWebViewClient(new WebViewClient() {
//            ProgressDialog progressDialog;
//
//
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//
//                if (url.startsWith("tel:")) {
//                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
//                    startActivity(intent);
//                } else {
//
//                    view.loadUrl(url);
//
//                }
//                return true;
//            }
//
//            //Show loader on url load
//            @Override
//            public void onLoadResource(WebView view, String url) {
////                if (progressDialog == null) {
////                    // in standard case YourActivity.this
////                    progressDialog = new ProgressDialog(ShowWebView.this);
////                    progressDialog.setMessage("Loading...");
////                    progressDialog.show();
////                }
//            }
//
//            @Override
//            public void onPageFinished(WebView view, String url) {
////
//            }
//
//        });
//
//
//        // Javascript inabled on webview
//        webView.getSettings().setJavaScriptEnabled(true);
//
//        webView.addJavascriptInterface(new WebViewScriptInterface(ShowWebView.this, ShowWebView.this, webView, proxyInfoId), "share");
////
//        webView.loadUrl(url);
//
//
//    }
//
//    @Click
//    void btn_back() {
//        onBackPressed();
//    }
//
////    @Override
////    public void onBackmyPressed() {
////        // TODO Auto-generated method stub
////        super.onBackPressed();
////        Intent data = new Intent();
////        setResult(RESULT_CANCELED, data);
////        this.finish();
////    }
//
//    @Override
//    // Detect when the back button is pressed
//    public void onBackPressed() {
//        if (webView.canGoBack()) {
//            webView.goBack();
//        } else {
//            // Let the system handle the back button
//            super.onBackPressed();
//            Intent data = new Intent();
//            setResult(RESULT_CANCELED, data);
//            this.finish();
//        }
//    }

}